package com.sky.controller.admin;



import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Slf4j
@Api(tags = "分类相关的接口")
@RequiredArgsConstructor
public class CategoryController {


    private final CategoryService categoryService;

    /**
     * 实现根据分类名查询
     * @param categoryPageQueryDTO
     * @return
     */

    @ApiOperation("查询分类接口")
    @GetMapping("/page")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageResult pageResult = categoryService.queryCategory(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据类型查询相当于回显
     * @param type
     * @return
     */

    @ApiOperation("根据类型查询分类接口")
    @GetMapping("/list")
    public Result<List<Category>> list(String type) {
        List<Category> list = categoryService.list(type);
        return Result.success(list);
    }

    /**
     * 新增分类
     */
    @ApiOperation("新增分类接口")
    @PostMapping
    public Result save(@RequestBody CategoryDTO categoryDTO) {
        categoryService.save(categoryDTO);
        return Result.success();
    }

    /**
     * 删除分类
     */
    @ApiOperation("删除分类接口")
    @DeleteMapping
    public Result delete(Long id) {
        categoryService.delete(id);
        return Result.success();
    }
    /**
     * 编辑分类
     */
    @ApiOperation("编辑分类接口")
    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO) {
        categoryService.update(categoryDTO);
        return Result.success();
    }
    /**
     * 禁用与启动分类
     */
    @ApiOperation("禁用与启动分类接口")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status , Long id) {
        categoryService.updateStatus(status,id);
        return Result.success();
    }
}
